package com.ruoyi.llm.service;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.llm.constant.RabbitMqConstants;
import com.ruoyi.llm.dao.KnowledgeDocInfoMapper;
import com.ruoyi.llm.domain.po.KnowledgeDocInfoPo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
@DataSource(DataSourceType.MASTER)
public class KnowledgeDocInfoService  extends ServiceImpl<KnowledgeDocInfoMapper, KnowledgeDocInfoPo> {


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisMqService redisUtils;

    public KnowledgeDocInfoPo getknowledgeDoc(String docId){
        return this.getById(docId);
    }

    public List<KnowledgeDocInfoPo> knowledgeDocList(String knowledgeId){
        LambdaQueryWrapper<KnowledgeDocInfoPo> query=new LambdaQueryWrapper<>();
        query.eq(KnowledgeDocInfoPo::getKnowledgeId,knowledgeId);
        return this.list(query);

    }


    public String   sentMQ(String taskId,JSONObject json){
        String redisValue;
        System.out.println("======JSON:"+json.toString().replaceAll("\\\\","")+"========");
        rabbitTemplate.convertAndSend(RabbitMqConstants.TOPIC_AI_EXCHANGE,RabbitMqConstants.ROUTING_KEY_KNOWLEDGE_DOC, json.toString().replaceAll("\\\\","").getBytes(StandardCharsets.UTF_8));
        redisValue = redisUtils.BtrOver(taskId,5000);
        if(StringUtils.isBlank(redisValue)){
            redisValue = "设置超时";
        }
        return  redisValue;
    }
}
